<template>
  <div>
    <ul class="oul">
      <li v-for="(item, index) in $store.state.carList" :key="item.id">
        <div class="left">
          <input v-model="item.checked" @change="itemChange" type="checkbox" />
        </div>
        <div class="right">
          <h5>
            {{ item.title }}
          </h5>
          <h4>
            {{ item.price.toFixed(2) }}
          </h4>
          <h3 class="orange">
            {{ item.count }}
          </h3>
          <div>
            <button @click="addItem(index)">加</button> --
            <button @click="delItem(index)">减</button>
          </div>
        </div>
      </li>
    </ul>

    <div v-show="$store.getters.allCount > 0">
      <h2>总价: {{ $store.getters.allPrice.toFixed(2) }}</h2>
    </div>
    <div>
      全选: <input @change="changAllCheck" v-model="all" type="checkbox" />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      all: true,
    };
  },
  methods: {
    // 对购物车总的数量进行添加
    addItem(index) {
      this.$store.commit("addCarItemCount", index);
    },
    // 对购物车总的数量进行减少
    delItem(index) {
      this.$store.commit("delCarItemCount", index);
    },
    // 点击全选按钮  把当前的状态告诉carList
    changAllCheck() {
      this.$store.commit("changeChecked", this.all);
      console.log(this.all);
    },
    // 根据购物车中所有选中的状态判断，判断全选按钮的状态
    itemChange() {
      this.all = this.$store.state.carList.every(item => item.checked)
    }
  },
};
</script>

<style>
li {
  display: flex;
}

.left {
  flex: 1;
}

.left img {
  width: 100%;
}

.right {
  flex: 3;
}
</style>